<p:config xmlns:p="http://www.orbeon.com/oxf/pipeline" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:oxf="http://www.orbeon.com/oxf/processors" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xxforms="http://orbeon.org/oxf/xml/xforms" xmlns:xdb="http://orbeon.org/oxf/xml/xmldb" xmlns:xu="http://www.xmldb.org/xupdate">

	<p:param type="output" name="data"/>

	<p:processor name="oxf:request">
		<p:input name="config">
			<config >
				<include>/request/parameters</include>
			</config>
		</p:input>
		<p:output name="data" id="request"/>
	</p:processor>

	<p:processor name="oxf:xslt">
		<p:input name="data" href="#request"/>
		<p:output name="data" id="query"/>
		<p:input name="config">
			<xdb:query xsl:version="2.0" collection="/db/portal">
				
				<xsl:text>xquery version "1.0";</xsl:text>
				let $cat := '<xsl:value-of select="normalize-space(replace(/request/parameters/parameter[name='category']/value,'''', ''''''))"/>'
				let $listid := /widgetsList/category[exists(@id)]/@id
				let $id := number(max(($listid))) + 1
				return
				if (exists(/widgetsList/category/name[text()= $cat])) then
				<error>The category '{$cat}' already exists.</error>
				else 
					if ($cat = '') then 
					<error>Please provide a category name.</error>
					else
					<message>{
					update insert 
						<category>
						{attribute id {$id}}
							<name>{$cat}</name>
						</category>
						into /widgetsList}The category '{$cat}' has been successfuly created.</message>
			</xdb:query>
		</p:input>
	</p:processor>

	<p:processor name="oxf:xmldb-query">
		<p:input name="datasource" href="/config/datasource.xml"/>
		<p:input name="query" href="#query"/>
		<p:output name="data" id="message"/>
	</p:processor>

	<p:processor name="oxf:pipeline">
		<p:input name="config" href="../categories.xpl"/>
		<p:output name="data" id="datas"/>
	</p:processor>

	<p:processor name="oxf:identity">
		<p:input name="data" href="aggregate('data', #message, #datas#xpointer(/data/*))"/>
		<p:output name="data" ref="data"/>
	</p:processor>
</p:config>